import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/charts/levelProgressBar.dart';
import 'package:ichinesereader/widgets/grids/bookGridWithCheckBox.dart';
import 'package:ichinesereader/constants.dart';

class LeveledReadingBody extends StatefulWidget {
  LeveledReadingBody({
    Key? key,
  }) : super(key: key);

  _LeveledReadingBody createState() => new _LeveledReadingBody();
}

class _LeveledReadingBody extends State<LeveledReadingBody> {
  final BooksController _booksController = Get.find();
  final UserController _userController = Get.find();
  final box = Hive.box('appconfig');

  int limit = 20;
  int selectedPageNumber = 1;

  @override
  void initState() {
    Future.delayed(Duration(milliseconds: 100), () {
      bool isStudentOrIndividual =
          box.get(CacheManagerKey['nanhairole'].toString()) ==
                  Constants.NanhaiRole_Individual ||
              box.get(CacheManagerKey['nanhairole'].toString()) ==
                  Constants.NanhaiRole_Student;
      if (isStudentOrIndividual) {
        _booksController.fetchProgressReadingItems(limit, 0);
        _userController.getProfile();
        _userController.refreshLevelPercentage();
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    _booksController.selectedLevelReadingBookItemsClear();
    _booksController.leveledReadingBookItems.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build ProgressReadingBody');

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Container(
            margin: EdgeInsets.only(bottom: 9.0),
            width: Get.width - 60,
            height: 26,
            child: LevelProgressIndicatorWidget()),
        Flexible(
          child: Center(
            child: _booksController.obx(
              (data) => BookGridWithCheckBox(
                books: _booksController.leveledReadingBookItems,
                readingType: NanhaiBookReadingTypes.leveled,
              ),
              onEmpty: Center(child: Text('Books not available'.tr)),
              onLoading: Container(),
            ),
          ),
        ),
        // Container(
        //   child: Center(
        //     child: Padding(
        //       padding: const EdgeInsets.only(top: 12.0),
        //       child: _booksController.obx(
        //         (data) => NumberPagination(
        //           onPageChanged: (int pageNumber) {
        //             //got new books for selected page
        //             setState(() {
        //               selectedPageNumber = pageNumber;
        //               _booksController.fetchProgressReadingItems(
        //                   limit, selectedPageNumber - 1);
        //             });
        //           },
        //           // pageTotal:
        //           //     (_booksController.leveledBooksTotalNum.value / limit + 1)
        //           //         .toInt(),
        //           pageTotal: _booksController.leveledBooksTotalNum.value %
        //                       limit ==
        //                   0
        //               ? _booksController.leveledBooksTotalNum.value ~/ limit
        //               : _booksController.leveledBooksTotalNum.value ~/ limit +
        //                   1,
        //           threshold: 5,
        //           pageInit: selectedPageNumber,
        //           // picked number when init page
        //           colorPrimary: Colors.white,
        //           colorSub: Colors.blue,
        //         ),
        //       ),
        //     ),
        //   ),
        // ),
      ],
    );
  }
}
